﻿


var mEvidence = {};//物品
var mEvidence_IsNew = false;
var mEvidenceEstimate = null;
$(function () {
    try {

        extendEasyUI();

        var args = getUrlArgs();

        var index = args["index"];

        mEvidenceEstimate = args["mEvidenceEstimate"];
        if (index == -1) {
            mEvidence_IsNew = true;
            initEvidence();
        } else {
            mEvidence_IsNew = false;
            mEvidence = window.parent.mEvidences[index];
        }

        //默认隐藏车辆Tab
        $("#tabInfo").tabs('getTab', '车辆信息').panel('options').tab.hide();

        $("#cmbEvidenceType").combobox({
            valueField: 'Id',
            textField: 'Name',
            editable: false,
            panelHeight: 120,
            onSelect: onSelectEvidenceType
        });

        $("#cmbUM").combobox({
            valueField: 'Id',
            textField: 'Name',
            editable: false,
            panelHeight: 120,
            //onSelect: function (record) { mEvidence.UM = record; }
        });

        $("#cmbOrigin").combobox({
            valueField: 'Id',
            textField: 'Name',
            editable: false,
            panelHeight: 70,
            //onSelect: function (record) { mEvidence.Origin = record; }
        });

        $('#txtQty').numberbox({
            onChange: function (newValue, oldValue) {
                $('#txtAmount').numberbox('setValue', $('#txtQty').numberbox("getValue") * $('#txtPrice').numberbox("getValue"))
            }
        });

        $('#txtPrice').numberbox({
            onChange: function (newValue, oldValue) {
                $('#txtAmount').numberbox('setValue', $('#txtQty').numberbox("getValue") * $('#txtPrice').numberbox("getValue"))
            }
        });
        $('#txtSeizureDay').numberbox({
            onChange: function (newValue, oldValue) {
                $('#txtDurationTime').val(getEstimateTime(mEvidenceEstimate, newValue));
            }
        });

        //$('#txtQty').bind('blur', function () {
        //    $('#txtAmount').numberbox('setValue', $('#txtQty').numberbox("getValue") * $('#txtPrice').numberbox("getValue"))
        //});

        //$('#txtPrice').bind('blur', function () {
        //    $('#txtAmount').numberbox('setValue', $('#txtQty').numberbox("getValue") * $('#txtPrice').numberbox("getValue"))
        //});

        loadBaseData();

        dispaly();
    } catch (ex) {
        alert(ex);
    }
});
//初始化物品对象
function initEvidence() {

    mEvidence.BillId = window.parent.mEvidenceBill.Id;
    mEvidence.Id = 0;
    mEvidence.EvidenceType = null;
    mEvidence.Name = '';
    mEvidence.Model = null;
    mEvidence.Qty = 1;
    mEvidence.UM = null;
    mEvidence.Price = 0;
    mEvidence.Amount = 0;
    mEvidence.Feature = null;
    mEvidence.Description = null;
    mEvidence.OwnerName = null;
    mEvidence.OwnerContact = null;
    mEvidence.OwnerAddress = null;
    mEvidence.StorageState = 0;   //需要再确认
    mEvidence.Origin = null;
    mEvidence.SeizureDay = loadDefaultOutStockDeadline();
    mEvidence.VehicleFactory = null;
    mEvidence.VehicleNameplate = null;
    mEvidence.VehicleColor = null;
    mEvidence.VehicleUsage = null;
    mEvidence.VehicleIdCode = null;
    mEvidence.VehicleEngineIdCode = null;
    mEvidence.VehicleLicenceAuthority = null;
    mEvidence.VehicleNo = null;

    //添加serialNo 2014-07-17 owen
    mEvidence.SerialNo = null;
}

//选择物品类型
function onSelectEvidenceType(record) {
    if (record && record.Category == 1) {
        $("#tabInfo").tabs('getTab', '车辆信息').panel('options').tab.show();
    } else {
        $("#tabInfo").tabs('getTab', '车辆信息').panel('options').tab.hide();
    }
}
//显示
function dispaly() {

    lockElements(window.parent.mEvidenceBill.IsCheck);

    if (mEvidence.EvidenceType)
        $('#cmbEvidenceType').combobox("select", mEvidence.EvidenceType.Id);

    $('#txtName').val(mEvidence.Name);
    $('#txtModel').val(mEvidence.Model);
    $('#txtFeature').val(mEvidence.Feature);
    $('#txtSerialNo').val(mEvidence.SerialNo);

    if (mEvidence.UM)
        $('#cmbUM').combobox("select", mEvidence.UM.Id);

    $('#txtQty').numberbox('setValue', mEvidence.Qty);
    $('#txtStockQty').numberbox('setValue', mEvidence.StockQty);
    $('#txtOutStockQty').numberbox('setValue', mEvidence.OutStockQty);
    $('#txtInquiryOutStockQty').numberbox('setValue', mEvidence.InquiryOutStockQty);
    $('#txtPrice').numberbox('setValue', mEvidence.Price);
    $('#txtAmount').numberbox('setValue', mEvidence.Amount);
    $('#txtOwnerName').val(mEvidence.OwnerName);
    $('#txtOwnerContact').val(mEvidence.OwnerContact);
    $('#txtOwnerAddress').val(mEvidence.OwnerAddress);
    $('#txtSeizureDay').numberbox('setValue', mEvidence.SeizureDay);
    $('#txtEstimateTime').val(getEstimateTime(mEvidenceEstimate, mEvidence.SeizureDay));

    if (mEvidence.Origin)
        $('#cmbOrigin').combobox("select", mEvidence.Origin.Id);

    //车辆信息
    $('#txtVehicleNo').val(mEvidence.VehicleNo);
    $('#txtVehicleFactory').val(mEvidence.VehicleFactory);
    $('#txtVehicleNameplate').val(mEvidence.VehicleNameplate);
    $('#txtVehicleColor').val(mEvidence.VehicleColor);
    $('#txtVehicleUsage').val(mEvidence.VehicleUsage);
    $('#txtVehicleIdCode').val(mEvidence.VehicleIdCode);
    $('#txtVehicleEngineIdCode').val(mEvidence.VehicleEngineIdCode);
    $('#txtVehicleLicenceAuthority').val(mEvidence.VehicleLicenceAuthority);
}

function lockElements(lock) {
    if (lock) {
        $('#btnConfirm').hide();
        $('#cmbEvidenceType').combobox("disable", true);
        $('#txtName').attr("disabled", true);
        $('#txtModel').attr("disabled", true);
        $('#txtFeature').attr("disabled", true);
        $('#txtSerialNo').attr("disabled", true);
        $('#cmbUM').combobox("disable", true);
        $('#txtQty').attr("disabled", true);

        $('#txtStockQty').attr("disabled", true);
        $('#txtOutStockQty').attr("disabled", true);
        $('#txtInquiryOutStockQty').attr("disabled", true);
        $('#txtPrice').attr("disabled", true);
        $('#txtAmount').attr("disabled", true);
        $('#txtOwnerName').attr("disabled", true);
        $('#txtOwnerContact').attr("disabled", true);
        $('#txtOwnerAddress').attr("disabled", true);
        $('#txtSeizureDay').attr("disabled", true);
        $('#txtEstimateTime').attr("disabled", true);
        $('#cmbOrigin').combobox("disable", true);
        $('#txtVehicleNo').attr("disabled", true);
        $('#txtVehicleFactory').attr("disabled", true);
        $('#txtVehicleNameplate').attr("disabled", true);
        $('#txtVehicleColor').attr("disabled", true);
        $('#txtVehicleUsage').attr("disabled", true);
        $('#txtVehicleIdCode').attr("disabled", true);
        $('#txtVehicleEngineIdCode').attr("disabled", true);
        $('#txtVehicleLicenceAuthority').attr("disabled", true);
    }
}

//读取基础资料
function loadBaseData() {
    try {
        var origins = gLoadOrigins();
        $('#cmbOrigin').combobox("loadData", origins);

        var evidenceTypes = gLoadEvidenceTypes();
        $('#cmbEvidenceType').combobox("loadData", evidenceTypes);

        var ums = gLoadUMs();
        $('#cmbUM').combobox("loadData", ums);
    } catch (ex) {
        alert(ex);
    }
}

function viewToObj() {
    //基础信息
    mEvidence.EvidenceType = $('#cmbEvidenceType').combobox('getSelected');
    mEvidence.UM = $('#cmbUM').combobox('getSelected');;
    mEvidence.Origin = $('#cmbOrigin').combobox('getSelected');
    mEvidence.Name = $('#txtName').val();
    mEvidence.Model = $('#txtModel').val();
    mEvidence.Feature = $('#txtFeature').val();
    mEvidence.Qty = $('#txtQty').numberbox('getValue');
    mEvidence.Price = $('#txtPrice').numberbox('getValue');
    mEvidence.Amount = mEvidence.Qty * mEvidence.Price;
    mEvidence.OwnerName = $('#txtOwnerName').val();
    mEvidence.OwnerContact = $('#txtOwnerContact').val();
    mEvidence.OwnerAddress = $('#txtOwnerAddress').val();
    mEvidence.SeizureDay = $('#txtSeizureDay').numberbox('getValue');
    //车辆信息
    mEvidence.VehicleNo = $('#txtVehicleNo').val();
    mEvidence.VehicleFactory = $('#txtVehicleFactory').val();
    mEvidence.VehicleNameplate = $('#txtVehicleNameplate').val();
    mEvidence.VehicleColor = $('#txtVehicleColor').val();
    mEvidence.VehicleUsage = $('#txtVehicleUsage').val();
    mEvidence.VehicleIdCode = $('#txtVehicleIdCode').val();
    mEvidence.VehicleEngineIdCode = $('#txtVehicleEngineIdCode').val();
    mEvidence.VehicleLicenceAuthority = $('#txtVehicleLicenceAuthority').val();

    //添加 serialNO 2014-07-17 owen
    mEvidence.SerialNo = $('#txtSerialNo').val();
}
//检查物品字段
function checkEvidence() {

    if (mEvidence.EvidenceType == undefined) {
        $.messager.alert('提示', '请输入物品类型', 'warning');
        return false;
    }

    if (mEvidence.UM == undefined || mEvidence.UM == null) {
        $.messager.alert('提示', '请输入计量单位', 'warning');
        return false;
    }

    if (mEvidence.Qty == undefined || mEvidence.Qty == null || mEvidence.Qty == 0) {
        $.messager.alert('提示', '数量不能为零', 'warning');
        return false;
    }

    if (mEvidence.OwnerName == undefined || mEvidence.OwnerName == null || mEvidence.OwnerName == "") {
        $.messager.alert('提示', '请输入所有人姓名', 'warning');
        return false;
    }
    /*
    if (mEvidence.OwnerContact == undefined || mEvidence.OwnerContact == null || mEvidence.OwnerContact == "") {
        $.messager.alert('提示', '请输入所有人联系方式', 'warning');
        return false;
    }
    */

    //如果物品类型等于车辆
    if (mEvidence.EvidenceType != undefined && mEvidence.EvidenceType.Category == 1) {
        if (mEvidence.VehicleNo == undefined || mEvidence.VehicleNo == '') {
            $.messager.alert('提示', '请输入车牌号码', 'warning');
            return false;
        }
        if (mEvidence.VehicleColor == undefined || mEvidence.VehicleColor == '') {
            $.messager.alert('提示', '请输入车辆颜色', 'warning');
            return false;
        }
    }

    if (mEvidence.Name == undefined || mEvidence.Name == '') {
        $.messager.alert('提示', '请输入物品名称', 'warning');
        return false;
    }

    if (mEvidence.Qty == undefined || mEvidence.Qty == 0) {
        $.messager.alert('提示', '请输入物品数量', 'warning');
        return false;
    }

    if (mEvidence.OwnerName == undefined || mEvidence.Qty == '') {
        $.messager.alert('提示', '请输入所有人名称', 'warning');
        return false;
    }

    return true;
}
//保存物品
function confirmAction() {
    try {
        viewToObj();

        if (checkEvidence()) {
            if (mEvidence_IsNew) {
                window.parent.mEvidences.push(mEvidence);
            }

            window.parent.loadDetails();
            window.parent.mEvidenceDlg.close();
        }
    } catch (ex) {
        alert(ex);
    }

}
//关闭窗口
function exitAction() {
    window.parent.mEvidenceDlg.close();
}

function getEstimateTime(cTime, days) {
    var cETime = new Date(parseInt(cTime));
    var nt = cETime.setDate(cETime.getDate()+days);
    var newTime = new Date(nt);
    //var newTime = new Date(cETime);
    s = newTime.getFullYear() + "-" + (newTime.getMonth() + 1) + "-" + (newTime.getDate());
    return s;
};
function loadDefaultOutStockDeadline() {

    var deadLine = 0;

    $.ajax({
        url: "/System/LoadSystemParameter",
        type: "POST",
        async: false,
        dataType: "json",
        data: { "systemParameterKey": "OutStockDeadline" },
        success: function (req) {
            if (req.IsSuccess) {
                var result = JSON.parse(req.Data);
                deadLine = parseInt(result.Value);

            } else {
                $.messager.alert('提示', req.Data);
            }
        },
    });

    return deadLine;
}